package com.ruoyi.web.controller.file;

import com.ruoyi.common.core.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;


/**
 * 股票模块控制类
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/config")
public class StockMoudle extends BaseController{
    public static void main(String[] args) {
        List<String[]> stgf = new ArrayList<>();
        stgf.add(new String[]{"4.13","1400"});



        BigDecimal costPrice = new StockUtil().calculateStockCostPrice(stgf);
        System.out.println(costPrice);
    }
}

class StockUtil{

    // INSERT INTO `db_quant`.`stock_transactions`
    // (`transaction_id`, `stock_name`, `stock_code`, `transaction_date`, `trader_id`, `transaction_price`, `transaction_quantity`)
    // VALUES (1, '山推股份', '000680', '2025-02-12 16:34:45', 'admin', 10.01, 100);


    /**
     * 计算股票当前成本价格
     * @param data (String[1]价格 String[2]数量)
     * @return 成本价
     */
    public BigDecimal calculateStockCostPrice(List<String[]> data){
        BigDecimal nowPrice = new BigDecimal(data.get(0)[0]);//当前成本价 = 首次购入价格
        BigDecimal nowQuantity = new BigDecimal(data.get(0)[1]);//当前持仓数量 = 首次购入数量
        BigDecimal nowFunds = new BigDecimal("0").subtract(nowPrice.multiply(nowQuantity));//当前资金 = 0 - 首次购入价格 * 首次购入数量
        System.out.println("第" + (1) + "次交易\t交易价格：" + nowPrice + "\t交易数量：" + nowQuantity + "\t持仓数量：" + nowQuantity + "\t交易后余额：" + nowFunds);

        for(int i = 1 ; i < data.size() ; i++){
            BigDecimal price = new BigDecimal(data.get(i)[0]); //价格
            BigDecimal quantity = new BigDecimal(data.get(i)[1]);//操作数量




            if(quantity.compareTo(BigDecimal.ZERO) > 0){
                //买入后更新成本价 = 当前交易价格  - （（资金余额 + 当前交易价格 * 当前持仓数量）  /  （当前交易数量 + 当前持仓数量））
                BigDecimal temp1 = nowFunds.add( price.multiply(nowQuantity) ); // 资金余额 + 当前交易价格 * 当前持仓数量
                BigDecimal temp2 = temp1.divide(nowQuantity.add(quantity),4, RoundingMode.HALF_UP);
                nowPrice = price.subtract(temp2);
            }else if(quantity.compareTo(BigDecimal.ZERO) < 0 && nowQuantity.add(quantity).compareTo(BigDecimal.ZERO) > 0 ){
                //卖出后更新成本价 = （资金余额 + 当前交易价格 * 当前持仓数量）  /  当前持仓数量

                BigDecimal temp1 = nowFunds.add( price.multiply(nowQuantity) ); // 资金余额 + 当前交易价格 * 当前持仓数量
                BigDecimal temp2 = temp1.divide(nowQuantity.add(quantity),4, RoundingMode.HALF_UP);
                nowPrice = price.subtract(temp2);
            }


            //计算资金余额
            nowFunds = nowFunds.subtract(price.multiply(quantity));//资金余额

            //扣除手续费
            BigDecimal sxf = price.multiply(quantity).multiply(new BigDecimal("0.0001"));//手续费
            if(sxf.compareTo(new BigDecimal("5")) < 0){
                sxf = new BigDecimal("5");
            }
            nowFunds = nowFunds.subtract(sxf);

            //扣除印花税
            if(quantity.compareTo(BigDecimal.ZERO) < 0){
                BigDecimal yhs = price.multiply(quantity).multiply(new BigDecimal("0.001"));//手续费
                nowFunds = nowFunds.subtract(yhs);
            }

            //计算持仓数量
            nowQuantity = nowQuantity.add(quantity);//持仓数量




            System.out.println("第" + (i + 1) + "次交易\t交易价格：" + price + "\t交易数量：" + quantity + "\t持仓数量：" + nowQuantity + "\t交易后余额：" + nowFunds + "\t成本价：" + nowPrice + "\t成本差额：" + price.subtract(nowPrice));


        }




        return nowPrice;
    }

}

